<?php

namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);

use app\admin\Controller;
use think\Config;
use think\Db;
use think\Request;
use think\Validate;

class RouteCategory extends Controller
{
    use \app\admin\traits\controller\Controller;

    /**
     * 首页
     */
    public function index(Request $request)
    {
        $data = input();
        $map = [];
        $map['isdelete'] = 0;

        $listRows = $this->request->param('numPerPage') ? 20 : Config::get("paginate.list_rows");

        $list = Db::name('route_category')
            ->order('id desc')
            ->where($map)
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $category_list = Db::name('route_category')
            ->where('isdelete', 0)
            ->select();

        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        $this->view->assign('category_list', $category_list);
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        $data = input();

        if ($this->request->isAjax()) {

            $flag = Db::name('route_category')
                ->insert($data);

            if ($flag) {
                return ajax_return_adv('添加成功');
            } else {
                return ajax_return_adv_error('添加失败');
            }

        } else {
            return $this->view->fetch("edit");
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {

        $data = input();

        if ($this->request->isAjax()) {

            $flag = Db::name('route_category')
                ->where('id', $data['id'])
                ->setField('name', $data['name']);

            if ($flag) {
                return ajax_return_adv('编辑成功');
            } else {
                return ajax_return_adv_error('编辑失败');
            }

        } else {

            $vo = Db::name('route_category')
                ->where('id', $data['id'])
                ->find();

            $this->view->assign('vo', $vo);
            return $this->view->fetch();
        }
    }


    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id = input('id');
        $id = explode(',', $id);
        foreach ($id as $k => $v) {
            $up = Db::name('route_category')->where('id', $v)->update(['isdelete' => 1]);
        }
        if ($up) {
            return ajax_return_adv('移到回收站成功');
        } else {
            return ajax_return_adv_error('移到回收站失败');
        }
    }

    /**
     * 查询移到回收站的数据
     *
     */
    public function recyclebin()
    {
        $data = input();
        $map = [];
        $map['isdelete'] = 1;

        $listRows = $this->request->param('numPerPage') ? 20 : Config::get("paginate.list_rows");

        $list = Db::name('route_category')
            ->order('id desc')
            ->where($map)
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        return $this->view->fetch();
    }

    /**
     * 从回收站恢复数据
     *
     */
    public function recycle()
    {
        $id = input('id');
        $id = explode(',', $id);
        foreach ($id as $k => $v) {
            $up = Db::name('route_category')->where('id', $v)->update(['isdelete' => 0]);
        }
        if ($up) {
            return ajax_return_adv('恢复成功!');
        } else {
            return ajax_return_adv_error('恢复失败!');
        }
    }

    /**
     * 彻底删除数据
     *
     */
    public function deleteforever()
    {
        $id = input('id');
        $id = explode(',', $id);
        foreach ($id as $k => $v) {
            $up = Db::name('route_category')->delete($id);
        }
        if ($up) {
            return ajax_return_adv('删除数据成功!');
        } else {
            return ajax_return_adv_error('删除数据失败!');
        }
    }

    /**
     * 清空回收站
     */
    public function clear()
    {
        $del = Db::name('route_category')->where('isdelete', 1)->delete();
        if ($del) {
            return ajax_return_adv('清空回收站成功!');
        } else {
            return ajax_return_adv_error('清空回收站失败');
        }
    }

}